/**
 * Class: OpenLayers.Layer.WeMapABC
 * A class to exploring MapABC
 * zoom [0, 17]
 * Inherits from:
 *  - <OpenLayers.Layer.XYZ>
 */
OpenLayers.Layer.WeMapABC = OpenLayers.Class(OpenLayers.Layer.XYZ, {
    attribution: "Image Data - <a href='http://mapabc.com/'>MapABC</a>",
    sphericalMercator: true,
    wrapDateLine: true,
    
    url: [
        //'http://emap3.is.autonavi.com/appmaptile?z=4&x=9&y=10'
        'http://emap0.is.autonavi.com/appmaptile?z=${z}&x=${x}&y=${y}',
        'http://emap1.is.autonavi.com/appmaptile?z=${z}&x=${x}&y=${y}',
        'http://emap2.is.autonavi.com/appmaptile?z=${z}&x=${x}&y=${y}',
        'http://emap3.is.autonavi.com/appmaptile?z=${z}&x=${x}&y=${y}'
    ],
    
    initialize: function(name, url, options) {
        OpenLayers.Layer.XYZ.prototype.initialize.apply(this, arguments);
        this.name = name ? name : OpenLayers.i18n('WeMapABC_name');
    },
    
    getURL: function(bounds) {
        var res = this.map.getResolution();
        var x = Math.round((bounds.left - this.maxExtent.left) 
            / (res * this.tileSize.w));
        var y = Math.round((this.maxExtent.top - bounds.top) 
            / (res * this.tileSize.h));
        var z = (this.map.getZoom() + this.zoomOffset);
        
        var limit = Math.pow(2, z);
        if (this.wrapDateLine) {
           x = ((x % limit) + limit) % limit;
        }

        var url = this.url;
        var s = '' + x + y + z;
        if (url instanceof Array)
        {
            url = this.selectUrl(s, url);
        }
        
        var path = OpenLayers.String.format(url, {'x': x, 'y': y, 'z': z});

        return path;
    },
    
    clone: function(obj) {
        if (obj == null) {
            obj = new OpenLayers.Layer.WeMapABC(
                this.name, this.url, this.getOptions());
        }
        obj = OpenLayers.Layer.XYZ.prototype.clone.apply(this, [obj]);
        return obj;
    },
    
    CLASS_NAME: "OpenLayers.Layer.WeMapABC"
});

/**
 * Class: OpenLayers.Layer.WeMapABCTraffic
 * A class to exploring MapABC traffic data
 * zoom [7, 0]
 * Inherits from:
 *  - <OpenLayers.Layer.XYZ>
 */
OpenLayers.Layer.WeMapABCTraffic = OpenLayers.Class(OpenLayers.Layer.XYZ, {
    attribution: "Traffic Data - <a href='http://mapabc.com/'>MapABC</a>",
    sphericalMercator: true,
    wrapDateLine: true,
    isBaseLayer: false,
    visibility: false, // hide default
    
    url: [
        //'http://tm.mapabc.com/trafficengine/mapabc/traffictile?v=1.0&t=1&zoom=7&x=853&y=422'
        'http://tm.mapabc.com/trafficengine/mapabc/traffictile?v=1.0&t=1&zoom=${z}&x=${x}&y=${y}'
    ],
    
    initialize: function(name, url, options) {
        OpenLayers.Layer.XYZ.prototype.initialize.apply(this, arguments);
        this.name = name ? name : OpenLayers.i18n('WeMapABCTraffic_name');
    },
    
    getURL: function(bounds) {
        var res = this.map.getResolution();
        var x = Math.round((bounds.left - this.maxExtent.left) 
            / (res * this.tileSize.w));
        var y = Math.round((this.maxExtent.top - bounds.top) 
            / (res * this.tileSize.h));
        var z = (this.map.getZoom() + this.zoomOffset);
        
        var limit = Math.pow(2, z);
        if (this.wrapDateLine) {
           x = ((x % limit) + limit) % limit;
        }
        
        // zoom of this layer is [7, 0]
        z = 17 - z;

        var url = this.url;
        var s = '' + x + y + z;
        if (url instanceof Array)
        {
            url = this.selectUrl(s, url);
        }
        
        var path = OpenLayers.String.format(url, {'x': x, 'y': y, 'z': z});

        return path;
    },
    
    clone: function(obj) {
        if (obj == null) {
            obj = new OpenLayers.Layer.WeMapABCTraffic(
                this.name, this.url, this.getOptions());
        }
        obj = OpenLayers.Layer.XYZ.prototype.clone.apply(this, [obj]);
        return obj;
    },
    
    CLASS_NAME: "OpenLayers.Layer.WeMapABCTraffic"
});
